package example;

import java.sql.*;
import java.util.Calendar;

/**
 * Created by IntelliJ IDEA.
 * User: stermi
 * Date: 29-nov-2009
 * Time: 12.41.30
 * To change this template use File | Settings | File Templates.
 */
public class Test {
	
	static boolean DEBUG = true;

    public static void main(String[] args) {

        DBConnection db = new DBConnection();

        Connection c = db.getConnection();

        //testFilm( c );
        //testUser( c );
        
        testFattura( c );
        
        
        
        
        try {
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
        

    }
    
    public static void testFattura( Connection c ) {
    	
    	Result r = null;
    	Utente u = new Utente();
    	Film f = new Film();
    	Fattura fatt = new Fattura();
    	
    	Test.DEBUG = false;
    	
    	
    	
    	r = u.load(23, c);
    	System.out.println( u.toString() );
    	
    	r = testAddComplete(c);
    	f = (Film) r.value;
    	
    	Test.DEBUG = true;
    	
    	fatt.status = false;
    	Calendar cal = Calendar.getInstance();
    	fatt.dataNoleggio = new Date( cal.getTime().getTime() );
    	fatt.setFilm( f );
    	fatt.setUtente( u );
    	
    	System.out.println("Copie " + f.titolo + ": " + f.copieRimaste + "/" + f.copieTotali);
    	
    	r = fatt.save(c);
    	System.out.println( r.toString() );
    	
    	Film nf = new Film();
    	nf.load(f.id, c);
    	System.out.println("Copie " + nf.titolo + ": " + nf.copieRimaste + "/" + nf.copieTotali);
    	
    	
    	
    }
    
    public static void testUser( Connection c ) {
    	Result r = null;
    	Utente u = new Utente();
    	Utente ul = new Utente();
    	
    	Test.DEBUG = false;
    	
    	u.codiceCarta = "81265abhDe4587859676bFsCx";
    	u.nome = "Emanuele";
    	u.cognome = "Ricci";
    	u.compleanno = Utils.formatDate("03/09/1987", Utils.D_M_Y_FORMAT);
    	u.email = "stermi@gmail.com";
    	u.password = "tryme";
    	u.codiceFiscale = "RCCMNL87P03E715Z";
    	//u.codiceFiscale = "CODFISD7P03E715Z";
    	u.citta = "Segromigno in monte";
    	u.via = "via delle selvette, 320";
    	u.recapitoTelefonico = "3287698196";
    	u.roleId = 1;
    	
    	r = u.save(c);
    	if ( Test.DEBUG ) System.out.println( r.toString() );
    	
    	r = ul.load(u.id, c);
    	if ( r.result ) if ( Test.DEBUG ) System.out.println( ul.toString() );
    	
    	ul.email = "stermi@gmail.zom";
    	ul.nome = "nome modificato";
    	ul.cognome = "cognome modificato";
    	ul.codiceFiscale = "CODFISD7P03E715Z";
    	
    	r = ul.save(c);
    	if ( r.result ) 
    		if ( Test.DEBUG ) System.out.println( ul.toString() );
    	else 
    		if ( Test.DEBUG ) System.out.println( r.toString() );
    	
    	ul.email = "stermineitornuovo@gmail.com";
    	ul.nome = "nome modificato";
    	ul.cognome = "cognome modificato";
    	ul.codiceFiscale = "RCCMNL87P03E715Z";
    	
    	r = ul.save(c);
    	if ( r.result )
    		if ( Test.DEBUG ) System.out.println( ul.toString() );
    	else 
    		if ( Test.DEBUG ) System.out.println( r.errorMessage );
    	
    	r = ul.delete(c);
    	System.out.println( r.toString() );
    	
    	
    	return;
    	
    }
   
    public static void testFilm( Connection c ) {
        
        Film fAdd = null;
        Film fLoad = null;
        Result r = null;
        
        //testAddFilm(c);
        
        r = testAddComplete(c);
        if ( r != null && r.result ) {
        	fAdd = (Film) r.value;
        	if ( Test.DEBUG ) System.out.println( fAdd.toString() );
        } else 
        	if ( Test.DEBUG ) 
        		System.out.println( r.errorMessage );
        
        
        if ( r != null && r.result  ) {
        	
        	/* fAdd effettuo piccole modifiche sui campi significativi */
        	Film fUpdate = new Film();
        	
        	fUpdate.set( fAdd );
        	
        	fUpdate.titolo = "TITOLO MODIFICATO";
        	fUpdate.uscita = Utils.formatDate( "11/11/2011", Utils.D_M_Y_FORMAT );
        	fUpdate.visibile = false;
        	fUpdate.copieRimaste = -11; 
        	fUpdate.copieTotali = -33;
        	fUpdate.voto = 22; 
        	fUpdate.anno = 25; 
        	fUpdate.durata = -222; 
        	
        	r = testUpdate(c, fUpdate);

        	fUpdate.set( fAdd );
        	fUpdate.titolo = "TITOLO MODIFICATO";
        	fUpdate.descrizione = "Questa volta i membri dell'oggetto passano il controllo obbligato update";
        	
        	r = testUpdate(c, fUpdate);
        	
        	/* Adesso fAdd e' stato aggiornato */
        	
    		r = testLoad(c, fAdd.id );
	        if ( r != null && r.result ) {
	        	fLoad = (Film) r.value;
	        	
	        	if ( Test.DEBUG ) System.out.println( fLoad.toString() );    	        	
	        	Persona pp = new Persona();
	        	
	        	for ( Persona p: fLoad.registi) {
	        		pp.set(p);
	        		if ( Test.DEBUG ) System.out.println( pp.delete(c).errorMessage );	
	        	}
	        	for ( Persona p: fLoad.scrittori) {
    	        	pp.set(p);
    	        	if ( Test.DEBUG ) System.out.println( pp.delete(c).errorMessage );	
	        	}
	        	for ( Persona p: fLoad.attori) {
    	        	pp.set(p);
    	        	if ( Test.DEBUG ) System.out.println( pp.delete(c).errorMessage );	
	        	}
	        	
	        	
	        	r = fLoad.delete(c);
	        	if ( Test.DEBUG ) System.out.println( r.errorMessage );
	        } else 
	        	if ( Test.DEBUG ) 
	        		System.out.println( r.errorMessage );
        	
        	
	        
        }
        
        
        
    }
    
    public static Result testLoad( Connection c, int filmId ) { 
    	Result r;
    	Film f = new Film();
    	f.id = filmId;
    	r = f.load(filmId, c);
    	return r;
    	
    }
    
    public static Result testUpdate( Connection c, Film f ) { 
    	Result r;
    	
    	//di defaulte validate e' a true
    	r = f.save(c);
    	
    	if ( Test.DEBUG ) System.out.println(r.errorMessage);
   
    	return r;
    	
    }
    
    public static Result testAddComplete( Connection c ) { 
    	
    	Result result;
    	
    	/* Aggiungi un regista */
        Persona reg = new Persona();        
        reg.nome = "Alex";
        reg.cognome = "Proyas";
        reg.dataNascita = Utils.formatDate( "23/09/1963", Utils.D_M_Y_FORMAT );
        reg.luogoNascita = "Egypt";
        reg.biografia = "Like David Fincher and Michael Bay, Alex Proyas has moved effortlessly between helming TV commercials and music videos to feature films. To date, he has specialized in visually stunning action thrillers which utilize myth and iconography in compensation for any shortcomings in the script and/or acting. Born to Greek parents in Egypt, Proyas relocated to Australia with his family when he was three years old. He began making films at age ten and went on to attend the Australian Film Television and Radio School along with Jane Campion and Jocelyn Moorhouse. Proyas collaborated with Campion on two of her shorts, A Girl\'s Own Story (1984), for which he wrote and performed a song, and Passionless Moments (1983), which he photographed. Proyas\' own short, Groping (1980), had earned him some attention at festival screenings in Sydney and London. Also while still a student, the enterprising novice formed Meaningful Eye Contact, a production company. Spirits of the Air, Gremlins of the Clouds (1989) marked Proyas\' feature debut as director and screenwriter. Set in a post-apocalyptic world, the film, with its stylized production design and aural texture, was atypical of standard Australian fare, more closely resembling a longform music video. Critics admired the director\'s vision, but felt the overall result was lacking. Proyas continued to hone his craft helming TV advertisements for products like Nike, Nissan and Swatch (earning kudos from advertising associations in both Australia and England) and directing videos for such artists as Sting, INXS and Crowded House. In 1993, Proyas was tapped to helm the screen adaptation of James O\'Barr\'s comic strip The Crow (1994). While filming, lead actor Brandon Lee died of an accidental gunshot wound (ironically the film\'s story revolves around his character\'s resurrection). His death cast a pall over the remainder of the filming and its subsequent theatrical release, although reviews were generally favorably, most singling out the production values which created a colorless rain-soaked wasteland that invoked comparisons with Ridley Scott\'s seminal Blade Runner (1982) and Tim Burton\'s Batman (1989). Made for about $14 million, it grossed close to $50 million domestically. Proyas seemed set to move on to other projects and was announced as the director of Casper (1995), but left the project and was replaced by Brad Silberling. After a four year absence, he returned with another thriller, Dark City (1998), about an amnesiac who may or may not have been a serial killer. Garage Days (2002) marked Proyas\' return to his homeland, Australia: the movie tells the story of a young Sydney garage band desperately trying to make it big in the competitive world of rock \'n\' roll. In 2004 Proyas returned to Hollywood: he directed I, Robot (2004), a science fiction film suggested by the Isaac Asimov short story compilation of the same name that starred Will Smith. It was a box office success, but met with mixed reactions by readers and fans of the Asimov stories.";
        reg.awards = "5 wins & 4 nominations";
        
        //salvo regista
        result = reg.save(c);
        if ( !result.result ) {
        	if ( Test.DEBUG ) System.out.println( "[ERROR ADD REGISTA] " + result.errorMessage );
        	return null;
        } else
        	if ( Test.DEBUG ) 
        		System.out.println( "[OK ADD REGISTA] " + result.toString() );
        
        /* Aggiungi due scrittori */
        Persona scrit = new Persona();        
        scrit.nome = "Jeff";
        scrit.cognome = "Vintar";
        scrit.dataNascita = null;
        scrit.luogoNascita = null;
        scrit.biografia = "Jeff Vintar was born in 1964 in Oak Park, Illinois. He gave up a renowned bus driving career to attend the University of Iowa\'s Writers\' Workshop, and after that spent the next several years starving until he sold three spec scripts in 1995: \"The Long Hello and Short Goodbye\", \"Spaceless\" and \"Hardwired.\" He has since written screenplays for Warner Bros, Twentieth Century Fox, and Columbia Pictures.";
        scrit.awards = null;
        
        //salvo scrittore
        result = scrit.save(c);
        if ( !result.result ) {
        	if ( Test.DEBUG ) 
        		System.out.println( "[ERROR ADD SCRITTORE1] " + result.errorMessage );
        	return null;
        } else
        	if ( Test.DEBUG ) 
        		System.out.println( "[OK ADD SCRITTORE1] " + result.toString() );
        
        Persona scrit2 = new Persona();        
        scrit2.nome = "Akiva";
        scrit2.cognome = "Goldsman";
        scrit2.dataNascita = Utils.formatDate( "7/06/1962", Utils.D_M_Y_FORMAT );
        scrit2.luogoNascita = "New York City, New York, USA";
        scrit2.biografia = "Like David Fincher and Michael Bay, Alex Proyas has moved effortlessly between helming TV commercials and music videos to feature films. To date, he has specialized in visually stunning action thrillers which utilize myth and iconography in compensation for any shortcomings in the script and/or acting. Born to Greek parents in Egypt, Proyas relocated to Australia with his family when he was three years old. He began making films at age ten and went on to attend the Australian Film Television and Radio School along with Jane Campion and Jocelyn Moorhouse. Proyas collaborated with Campion on two of her shorts, A Girl\'s Own Story (1984), for which he wrote and performed a song, and Passionless Moments (1983), which he photographed. Proyas\' own short, Groping (1980), had earned him some attention at festival screenings in Sydney and London. Also while still a student, the enterprising novice formed Meaningful Eye Contact, a production company. Spirits of the Air, Gremlins of the Clouds (1989) marked Proyas\' feature debut as director and screenwriter. Set in a post-apocalyptic world, the film, with its stylized production design and aural texture, was atypical of standard Australian fare, more closely resembling a longform music video. Critics admired the director\'s vision, but felt the overall result was lacking. Proyas continued to hone his craft helming TV advertisements for products like Nike, Nissan and Swatch (earning kudos from advertising associations in both Australia and England) and directing videos for such artists as Sting, INXS and Crowded House. In 1993, Proyas was tapped to helm the screen adaptation of James O\'Barr\'s comic strip The Crow (1994). While filming, lead actor Brandon Lee died of an accidental gunshot wound (ironically the film\'s story revolves around his character\'s resurrection). His death cast a pall over the remainder of the filming and its subsequent theatrical release, although reviews were generally favorably, most singling out the production values which created a colorless rain-soaked wasteland that invoked comparisons with Ridley Scott\'s seminal Blade Runner (1982) and Tim Burton\'s Batman (1989). Made for about $14 million, it grossed close to $50 million domestically. Proyas seemed set to move on to other projects and was announced as the director of Casper (1995), but left the project and was replaced by Brad Silberling. After a four year absence, he returned with another thriller, Dark City (1998), about an amnesiac who may or may not have been a serial killer. Garage Days (2002) marked Proyas\' return to his homeland, Australia: the movie tells the story of a young Sydney garage band desperately trying to make it big in the competitive world of rock \'n\' roll. In 2004 Proyas returned to Hollywood: he directed I, Robot (2004), a science fiction film suggested by the Isaac Asimov short story compilation of the same name that starred Will Smith. It was a box office success, but met with mixed reactions by readers and fans of the Asimov stories.";
        scrit2.awards = "Won Oscar. Another 4 wins & 11 nominations";
        
        //salvo scrittore
        result = scrit2.save(c);
        if ( !result.result ) {
        	if ( Test.DEBUG ) 
        		System.out.println( "[ERROR ADD SCRITTORE2] " + result.errorMessage );
        	return null;
        } else
        	if ( Test.DEBUG ) 
        		System.out.println( "[OK ADD SCRITTORE2] " + result.toString() );
        
        /* Aggiungo due attori */
        
        Persona att = new Persona();        
        att.nome = "Will";
        att.cognome = "Smith";
        att.dataNascita = Utils.formatDate( "25/09/1968", Utils.D_M_Y_FORMAT );
        att.luogoNascita = "Philadelphia, Pennsylvania, USA";
        att.biografia = "Will Smith was the second of four children of Caroline (school board employee) and Willard C Smith Sr. (owner of a refrigeration company). Smith is of both African American and Native American heritage. He grew up in middle class area in West Philadelphia called Wynnefield. Will attended the Overbrook High School located in the Overbrook section of Philadelphia, Pennsylvania. He got the nickname 'Prince' because of the way he could charm his way out of trouble. Bright student Will also signed up with the high-status Julia Reynolds Masterman Laboratory and Demonstration School in Philadelphia.";
        att.awards = "Nominated for 2 Oscars. Another 40 wins & 63 nominations";
        
        //salvo attore
        result = att.save(c);
        if ( !result.result ) {
        	if ( Test.DEBUG ) 
        		System.out.println( "[ERROR ADD ATTORE1] " + result.errorMessage );
        	return null;
        } else
        	if ( Test.DEBUG ) 
        		System.out.println( "[OK ADD ATTORE1] " + result.toString() );
        
        Persona att2 = new Persona();        
        att2.nome = "Bridget";
        att2.cognome = "Moynahan";
        att2.dataNascita = Utils.formatDate( "28/04/1971", Utils.D_M_Y_FORMAT );
        att2.luogoNascita = "Binghamton, New York, USA";
        att2.biografia = null;
        att2.awards = null;
        
        
        //salvo attore
        result = att2.save(c);
        
        if ( !result.result ) {
        	if ( Test.DEBUG ) 
        		System.out.println( "[ERROR ADD ATTORE2] " + result.errorMessage );
        	return null;
        } else
        	if ( Test.DEBUG ) 
        		System.out.println( "[OK ADD ATTORE2] " + result.toString() );
        
        /* Aggiungo Film */        
        
        Film f = new Film();
        f.categoriaId = 1;
        f.registi.add( new Regista(reg, -1) );
        f.scrittori.add(new Scrittore(scrit, -1));
        f.scrittori.add(new Scrittore(scrit2, -1));
        f.attori.add(new Attore(att, -1));
        f.attori.add(new Attore(att2, -1));
        /* Caratteristiche film */
        f.titolo = "I, Robot";
        f.descrizione = "It's 2035 A.D., where robots are everyday objects and are programmed to live alongside humans. Detective Del Spooner is called out to investigate the apparent suicide of the scientist behind these robots, Dr. Alfred Lanning. Spooner suspects that the death might not be a suicide, but the result of one of the robots. All robots are programmed by three laws, but Spooner starts to wonder if a robot can in fact feel emotions, and possibly murder. But if Spooner's suspicions are true, he is going to have a hard time convincing everyone.";
        f.anno = 2004;
        f.uscita = Utils.formatDate( "22/10/2004", Utils.D_M_Y_FORMAT );
        f.voto = 7.0;
        f.durata = 115;
        f.copieTotali = 120;
        f.copieRimaste = 28;
        f.visibile = true;
        
        //salvo f
        
    	result = f.save(c, false);
        
        
        return result;
    }
    /* Completalo con gestione del result */
    public static void testAddFilmSimple( Connection c ) { 
    	
    	Result result;
    	
    	/* Aggiungo Film */        
        
        Film f = new Film();
        f.categoriaId = 1;
        /* Caratteristiche film */
        f.titolo = "I, Robot";
        f.descrizione = "It's 2035 A.D., where robots are everyday objects and are programmed to live alongside humans. Detective Del Spooner is called out to investigate the apparent suicide of the scientist behind these robots, Dr. Alfred Lanning. Spooner suspects that the death might not be a suicide, but the result of one of the robots. All robots are programmed by three laws, but Spooner starts to wonder if a robot can in fact feel emotions, and possibly murder. But if Spooner's suspicions are true, he is going to have a hard time convincing everyone.";
        f.anno = 2004;
        f.uscita = Utils.formatDate( "10/10/2012", Utils.D_M_Y_FORMAT );
        f.voto = 7.0;
        f.durata = 115;
        f.copieTotali = 120;
        f.copieRimaste = 28;
        f.visibile = true;
        
        //salvo f
        result = f.save(c, false);
        if ( !result.result ) {
        	if ( Test.DEBUG ) 
        		System.out.println( "[ERROR ADD FILM] " + result.errorMessage );
        	return ;
        } else
        	if ( Test.DEBUG ) 
        		System.out.println( "[OK ADD FILM] " + result.toString() );
        
        return;
    }
}
